﻿import { MTLLoader } from 'three-obj-mtl-loader'
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader'
import {  FBXLoader } from 'three/examples/jsm/loaders/FBXLoader'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'


// 加载obj,mtl文件
export function objloader(path) {
  return new Promise(resolve => {
    var mtlLoader = new MTLLoader()
    //  初始化obj
    var objLoader = new OBJLoader()
    // 加载mtl文件
    mtlLoader.load(`three/${path}.mtl`, (mtl) => {
      // 初始化
      mtl.preload()
      // 加载贴图
      objLoader.setMaterials(mtl)
      objLoader.load(`three/${path}.obj`, (obj) => {
        resolve({
          mtl,
          obj
        })
      })
    })
  })
}

// 加载fbx文件
export function fbxloader(path) {
  return new Promise(resolve => {
    var loader = new FBXLoader()
    loader.load(`three/${path}.fbx`, (fbx) => {
      resolve(fbx)
    }, function (xhr) {
      // 控制台查看加载进度xhr
      // 通过加载进度xhr可以控制前端进度条进度   Math.floor:小数加载进度取整
      console.log('加载进度' + Math.floor(xhr.loaded / xhr.total * 100) + '%');
    })
  })
}
// 加载gtlf文件
export function gltfloader(path) {
  return new Promise(resolve => {
    var loader = new GLTFLoader()
    loader.load(`three/${path}.gltf`, (gltf) => {
      resolve(gltf)
    })
  })
}
